package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.pojo.iPackage.CustomSpecsHistory;
import com.xthink.common.pojo.iPackage.PackageSpecs;
import com.xthink.common.vo.iPackage.InsertCustomSpecs;
import com.xthink.common.result.AjaxResult;
import com.xthink.web.mapper.CustomSpecsHistoryMapper;
import com.xthink.web.mapper.PackageSpecsMapper;
import com.xthink.web.service.PackageSpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PackageSpecsServiceImpl extends ServiceImpl<PackageSpecsMapper, PackageSpecs> implements PackageSpecsService {

    @Autowired
    private PackageSpecsMapper packageSpecsMapper;

    @Autowired
    private CustomSpecsHistoryMapper customSpecsHistoryMapper;

    //自定义查询套餐规格
    @Override
    public AjaxResult customPackageSpecsDetails(Integer id) {
        LambdaQueryWrapper<PackageSpecs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PackageSpecs::getPackageId, id);
        List<PackageSpecs> packageSpecs = packageSpecsMapper.selectList(wrapper);
        return AjaxResult.success(packageSpecs);
    }

    //自定义套餐规格上架
    @Override
    public AjaxResult customPackageSpecsPutOn(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",ids[i]).set("status",1);
            int update = packageSpecsMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("ID:"+ids[i]+"上架失败，该商品已上架");
            }
        }
        return AjaxResult.success();
    }

    //自定义套餐规格下架
    @Override
    public AjaxResult customPackageSpecsOff(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",ids[i]).set("status",0);
            int update = packageSpecsMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("ID:"+ids[i]+"下架失败，该商品已下架");
            }
        }
        return AjaxResult.success();
    }

    //修改自定义套餐价格
    @Override
    public AjaxResult customPackageSpecsUpdatePrice(Integer id, Double price) {
        UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("month_price",price);
        int update = packageSpecsMapper.update(null, wrapper);

        if (update==0){
            return AjaxResult.error("修改价格失败");
        }

        //添加给自定义套餐规格历史价格表
        LambdaQueryWrapper<CustomSpecsHistory> customSpecsHistoryUpdateWrapper = new LambdaQueryWrapper<>();
        customSpecsHistoryUpdateWrapper.eq(CustomSpecsHistory::getSpecsId,id);
        CustomSpecsHistory _customSpecsHistory = customSpecsHistoryMapper.selectOne(customSpecsHistoryUpdateWrapper);
        Integer month = _customSpecsHistory.getMonth();
        CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
        customSpecsHistory.setSpecsId(id);
        customSpecsHistory.setMonth(month);
        customSpecsHistory.setMonthPrice(price);
        customSpecsHistoryMapper.insert(customSpecsHistory);

        return AjaxResult.success();
    }

    //新建自定义套餐规格
    @Override
    public AjaxResult insertCustomSpecs(InsertCustomSpecs insertCustomSpecs) {

        PackageSpecs packageSpecs = new PackageSpecs();
        packageSpecs.setPackageId(insertCustomSpecs.getId());
        packageSpecs.setMonth(insertCustomSpecs.getMonth());
        packageSpecs.setMonthPrice(insertCustomSpecs.getMonthPrice());
        packageSpecs.setStatus(insertCustomSpecs.getStatus());
        int insert = packageSpecsMapper.insert(packageSpecs);

        if (insert==0){
            return AjaxResult.error("新建规格套餐失败");
        }

        //存入销售规格历史价格表
        Integer id = packageSpecs.getId();
        CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
        customSpecsHistory.setSpecsId(id);
        customSpecsHistory.setMonth(insertCustomSpecs.getMonth());
        customSpecsHistory.setMonthPrice(insertCustomSpecs.getMonthPrice());
        customSpecsHistoryMapper.insert(customSpecsHistory);
        return AjaxResult.success();
    }

    //标准套餐规格详情
    @Override
    public AjaxResult pipPackageSpecsDetails(Integer id) {
        LambdaQueryWrapper<PackageSpecs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PackageSpecs::getPackageId, id);
        List<PackageSpecs> packageSpecs = packageSpecsMapper.selectList(wrapper);
        return AjaxResult.success(packageSpecs);
    }

    //标准套餐规格上架
    @Override
    public AjaxResult pipPackageSpecsPutOn(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",ids[i]).set("status",1);
            int update = packageSpecsMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("ID:"+ids[i]+"上架失败，该商品已上架");
            }
        }
        return AjaxResult.success();
    }

    //标准套餐规格下架
    @Override
    public AjaxResult pipPackageSpecsOff(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",ids[i]).set("status",0);
            int update = packageSpecsMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("ID:"+ids[i]+"下架失败，该商品已下架");
            }
        }
        return AjaxResult.success();
    }

    //修改标准套餐规格价格
    @Override
    public AjaxResult pipPackageSpecsUpdatePrice(Integer id, Double price) {
        UpdateWrapper<PackageSpecs> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("month_price",price);
        int update = packageSpecsMapper.update(null, wrapper);

        if (update==0){
            return AjaxResult.error("修改价格失败");
        }

        //添加给标准套餐规格历史价格表
        LambdaQueryWrapper<CustomSpecsHistory> customSpecsHistoryUpdateWrapper = new LambdaQueryWrapper<>();
        customSpecsHistoryUpdateWrapper.eq(CustomSpecsHistory::getSpecsId,id);
        CustomSpecsHistory _customSpecsHistory = customSpecsHistoryMapper.selectOne(customSpecsHistoryUpdateWrapper);
        Integer month = _customSpecsHistory.getMonth();
        CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
        customSpecsHistory.setSpecsId(id);
        customSpecsHistory.setMonth(month);
        customSpecsHistory.setMonthPrice(price);
        customSpecsHistoryMapper.insert(customSpecsHistory);

        return AjaxResult.success();
    }

    //新增标准套餐规格
    @Override
    public AjaxResult insertPipSpecs(InsertCustomSpecs insertCustomSpecs) {
        PackageSpecs packageSpecs = new PackageSpecs();
        packageSpecs.setPackageId(insertCustomSpecs.getId());
        packageSpecs.setMonth(insertCustomSpecs.getMonth());
        packageSpecs.setMonthPrice(insertCustomSpecs.getMonthPrice());
        packageSpecs.setStatus(insertCustomSpecs.getStatus());
        int insert = packageSpecsMapper.insert(packageSpecs);

        if (insert==0){
            return AjaxResult.error("新建规格套餐失败");
        }

        //存入销售规格历史价格表
        Integer id = packageSpecs.getId();
        CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
        customSpecsHistory.setSpecsId(id);
        customSpecsHistory.setMonth(insertCustomSpecs.getMonth());
        customSpecsHistory.setMonthPrice(insertCustomSpecs.getMonthPrice());
        customSpecsHistoryMapper.insert(customSpecsHistory);
        return AjaxResult.success();
    }

    //卡片管理中绑定套餐规格下拉框
    @Override
    public AjaxResult binDingPackageSpecs(Integer id) {
        LambdaQueryWrapper<PackageSpecs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PackageSpecs::getPackageId, id);
        List<PackageSpecs> packageSpecs = packageSpecsMapper.selectList(wrapper);
        return AjaxResult.success(packageSpecs);
    }

}
